/*
 * =====================================================================================
 *
 *       Filename:  cl_ring_buffer.hxx
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  05/28/2011 01:38:08 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Ken Chen (Ken), chen_kenz@emc.com
 *        Company:  EMC
 *
 * =====================================================================================
 */

#ifndef _CL_RING_BUFFER_HXX_
#define _CL_RING_BUFFER_HXX_

#include <vector>

namespace neo
{
template<typename T>
class CLRingBuffer
{
public:
  CLRingBuffer(unsigned int siz) 
    : m_curIndex(0) 
  {
    m_rbuf.resize(siz);
  }

  ~CLRingBuffer()
  {
  }
  
  const T& back(void) const
  {
    return m_rbuf[m_curIndex]; 
  }

  T& back(void)
  {
    return m_rbuf[m_curIndex]; 
  }

  void push(const T& t)
  {
    // m_curIndex = (++m_curIndex)%m_rbuf.size();
    // GCC warning: error: operation on ...m_curIndex’ may be undefined
    ++m_curIndex;
    m_curIndex = m_curIndex % m_rbuf.size();
    m_rbuf[m_curIndex] = t;
  }

private:
  std::vector<T> m_rbuf;
  unsigned int m_curIndex;
};

} // neo

#endif //_CL_RING_BUFFER_HXX_

